package com.waitingsnow.modules.shell.view.baseView
{
import assets.shellView.BeginLiveAsset;
import assets.shellView.BuyAsset;
import assets.shellView.TicketStatusAsset;

import com.waitingsnow.core.model.config.AppConfig;
import com.waitingsnow.core.util.ds.ExternalUtil;
import com.waitingsnow.core.util.ds.StringUtil;
import com.waitingsnow.modules.shell.model.vo.RubyServerVO;
import com.waitingsnow.modules.shell.view.baseView.events.ActivityStatusViewEvent;

import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextFormat;

/**
 * 
 * @author waitingsnow
 * $Id: ActivityStatusView.as 2 2012-05-17 10:10:03Z cgslendy@gmail.com $
 */
public class ActivityStatusView extends Sprite
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	/**
	 * Constructs a new <code>ActivityStatusView</code> instance.
	 * 
	 */
	public function ActivityStatusView()
	{
		initDisplay();
	}
	
	//==========================================================================
	//  Variables
	//==========================================================================
	private var buyAsset:BuyAsset;
	private var beginLiveAsset:BeginLiveAsset;
	private var ticketStatusAsset:TicketStatusAsset;
	
	private var _rubyServerVO:RubyServerVO;
	private var tft:TextFormat;
	
	//==========================================================================
	//  Methods
	//==========================================================================
	private function initDisplay():void
	{
		
	}
	
	private function showActivityStatus(msg:String):void
	{
		if (!ticketStatusAsset)
		{
			ticketStatusAsset = new TicketStatusAsset();
			ticketStatusAsset.x = 210;
			ticketStatusAsset.y = 180;
			tft = new TextFormat();
			tft.letterSpacing = 8;
			addChild(ticketStatusAsset);
		}
		ticketStatusAsset.info_txt.text = msg;
		ticketStatusAsset.info_txt.setTextFormat(tft);
	}
	
	public function setActivityStatus(vo:RubyServerVO):void
	{
		_rubyServerVO = vo;
		if (vo.activityStates == 5)
		{
			showActivityStatus("已结束");
			return;
		}
		else if (vo.activityStates == 6)
		{
			showActivityStatus("已取消");
			return;
		}
		else if (vo.activityStates == 7)
		{
			showActivityStatus("已关闭");
			return;
		}
		if (vo.isMaster)
		{
			if (!beginLiveAsset)
			{
				beginLiveAsset = new BeginLiveAsset();
			}
			beginLiveAsset.x = 210;
			beginLiveAsset.y = 180;
			beginLiveAsset.addEventListener(MouseEvent.CLICK, beginLiveClickHandler);
			addChild(beginLiveAsset);
		}
		else
		{
			if (vo.hasTicket && (vo.activityStates == 4))
			{
				var str:String = "准备中";
				showActivityStatus(str);
			}
			else
			{
				if (!buyAsset)
				{
					buyAsset = new BuyAsset();
				}
				buyAsset.y = 172;
				var infoStr:String = buyAsset.info_txt.text;
				var beginDate:Date = new Date(vo.beginTime);
				var totalTime:String = (vo.totalTime >= 60) ? (int(vo.totalTime/60) + "小时") : "";
				totalTime += (vo.totalTime % 60) ? (vo.totalTime%60 + "分钟") : "";
				infoStr = StringUtil.substitute(infoStr, "<b>" + _rubyServerVO.price + "</b>", 
					beginDate.getFullYear(), 
					beginDate.getMonth() + 1, beginDate.getDate(), beginDate.getHours(), 
					beginDate.getMinutes(), totalTime, int(_rubyServerVO.price/10)/10);
				buyAsset.info_txt.htmlText = infoStr;
				buyAsset.buy_btn.addEventListener(MouseEvent.CLICK, buyTicketClickHandler);
				if (vo.hasTicket)
				{
					buyAsset.buy_btn.visible = false;
					var states_btn:TicketStatusAsset = new TicketStatusAsset();
					states_btn.x = buyAsset.buy_btn.x;
					states_btn.y = buyAsset.buy_btn.y;
					buyAsset.addChild(states_btn);
				}
				buyAsset.invite_btn.addEventListener(MouseEvent.CLICK, function inviteBtnClickHandler():void
				{
					ExternalUtil.call(AppConfig.GOTO_FREE_TICKETS_TO_JS);
				});
				buyAsset.activity_btn.addEventListener(MouseEvent.CLICK, function activityBtnClickHandler():void
				{
					ExternalUtil.call(AppConfig.GOTO_GROUP_TICKETS_TO_JS);
				});
				buyAsset.activity_btn.visible = vo.isElectronic;
				addChild(buyAsset);
			}
		}
	}
	
	public function resize():void
	{
		var wFlag:Boolean = AppConfig.wideFlag;
		if (ticketStatusAsset)
		{
			ticketStatusAsset.x = wFlag ? 380 : 210;
			ticketStatusAsset.y = wFlag ? 200 : 180
		}
		if (beginLiveAsset)
		{
			beginLiveAsset.x = wFlag ? 380 : 210;
			beginLiveAsset.y = wFlag ? 200 : 180
		}
		if (buyAsset)
		{
			buyAsset.x = wFlag ? 245 : 0;
			buyAsset.y = wFlag ? 200 : 180
		}
	}
	
	public function dispose():void
	{
		parent && parent.removeChild(this);
	}
	
	//==========================================================================
	//  Event Handlers
	//==========================================================================
	private function beginLiveClickHandler(event:MouseEvent):void
	{
		dispatchEvent(new ActivityStatusViewEvent(ActivityStatusViewEvent.START_LIVE_BTN_CLICK));
	}
	
	private function buyTicketClickHandler(event:MouseEvent):void
	{
		ExternalUtil.call(AppConfig.BUY_TICKET_TO_JS);
	}
}
}